package com.stylefeng.guns.biz.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.biz.api.service.ILotteryService;
import com.stylefeng.guns.biz.wxchat.dao.TWechatUserMapper;
import com.stylefeng.guns.biz.wxchat.model.TWechatUser;
import com.stylefeng.guns.common.constant.Enum.ActivityStatusEnum;
import com.stylefeng.guns.common.constant.Enum.AwardsTypeEnum;
import com.stylefeng.guns.common.constant.Enum.UserLotteryStatusEnum;
import com.stylefeng.guns.common.util.DrawLotteryUtil;
import com.stylefeng.guns.core.result.BaseResultVo;
import com.stylefeng.guns.core.result.JsonReturnHelper;
import com.stylefeng.guns.modular.platform.dao.ActivityMapper;
import com.stylefeng.guns.modular.platform.dao.AwardsMapper;
import com.stylefeng.guns.modular.platform.dao.UserLotteryMapper;
import com.stylefeng.guns.modular.platform.model.Activity;
import com.stylefeng.guns.modular.platform.model.Awards;
import com.stylefeng.guns.modular.platform.model.UserLottery;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yangliwei
 * @since 2018-07-26
 */
@Service
public class LotteryServiceImpl implements ILotteryService {
	
	@Autowired
	ActivityMapper activityMapper;
	
	@Autowired
	AwardsMapper awardsMapper;
	
	@Autowired
	UserLotteryMapper userLotteryMapper;
	
	@Autowired
	TWechatUserMapper tWechatUserMapper;

	/**
	 * 
	 */
	@Override
	@Transactional
	public BaseResultVo luckyDraw(Integer userId, Long activityId) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		Awards lotteryAwards =null;
		try{
			
			//活动
			Activity activity = activityMapper.selectById(activityId);
			if(activity ==null || activity.getStatus() == ActivityStatusEnum.INVAILD.getValue()){
				map.put("result", false);
				map.put("errorCode", "10001");
				map.put("errorMessage", "无效的活动");
				return JsonReturnHelper.getSuccessWithData(map);
			}
			
			//用户
			TWechatUser wechatUser = tWechatUserMapper.selectById(userId);
			if(wechatUser == null){
				map.put("result", false);
				map.put("errorCode", "10002");
				map.put("errorMessage", "无效的用户");
				return JsonReturnHelper.getSuccessWithData(map);
			}
			
			//扣减金币
			if(activity.getLotteryExpend() > wechatUser.getGoldCoin()){
				map.put("result", false);
				map.put("errorCode", "10003");
				map.put("errorMessage", "金币不足");
				return JsonReturnHelper.getSuccessWithData(map);
			}
			wechatUser.setGoldCoin(wechatUser.getGoldCoin() - activity.getLotteryExpend().intValue());
			tWechatUserMapper.updateById(wechatUser);
			
			//奖项
			EntityWrapper<Awards> wp = new EntityWrapper<Awards>();
			wp.eq("activity_id", activity.getId());
			wp.orderBy("winRate", true);
			List<Awards> list = awardsMapper.selectList(wp);
			if(list.isEmpty()){
				return  JsonReturnHelper.getFailWithError("没有活动对应的奖项", "10004");
			}
			
			//抽奖记录
			UserLottery userLottery = new UserLottery();
			
			//抽奖
			int index = DrawLotteryUtil.drawGift(list);
			if(index < 0){
				lotteryAwards = new Awards();
				lotteryAwards.setId(-1);
				lotteryAwards.setName("没有中奖");
				
				userLottery.setStatus(UserLotteryStatusEnum.LOSELOTTERY.getValue());
			}else{
				lotteryAwards = list.get(index);
				
				if(lotteryAwards.getType().equals(AwardsTypeEnum.JINBIN.getValue())){
					wechatUser.setGoldCoin(lotteryAwards.getValue().intValue());
					tWechatUserMapper.updateGoldCoin(wechatUser);
				}
				
				userLottery.setStatus(UserLotteryStatusEnum.WINLOTTERY.getValue());
			}
			
			userLottery.setUserId(userId);
			userLottery.setActivityId(activity.getId());
			userLottery.setAwardsId(list.get(index).getId());
			userLottery.setLotteryExpend(activity.getLotteryExpend());
			userLottery.setCreateId(String.valueOf(userId));
			userLottery.setCreateTime(new Date());
			userLottery.setUpdateTime(new Date());
			
			//新增抽奖记录
			userLotteryMapper.insert(userLottery);
			
		}catch(Exception e){
			return JsonReturnHelper.getFailWithError("服务器", "10006");
		}
		
		return JsonReturnHelper.getSuccessWithData(lotteryAwards);
	}

	@Override
	public BaseResultVo queryActivity(String appId) {
		
		Activity activity = null;
		try{
			Activity search = new Activity();
			search.setAppId(appId);
			search.setStatus(ActivityStatusEnum.VAILD.getValue());
			activity = activityMapper.selectOne(search);
		}catch(Exception e){
			JsonReturnHelper.getFailWithError("服务器异常", "505");
		}
		return JsonReturnHelper.getSuccessWithData(activity);
	}

	@Override
	public BaseResultVo queryAwardsList(Long activityId) {
		
		List<Awards> list = null;
		try{
			EntityWrapper<Awards> wp = new EntityWrapper<Awards>();
			wp.eq("activity_id", activityId);
			wp.orderBy("sort", true);
			list = awardsMapper.selectList(wp);
			if(list.isEmpty()){
				return  JsonReturnHelper.getFailWithError("没有活动对应的奖项", "10004");
			}
		}catch(Exception e){
			JsonReturnHelper.getFailWithError("服务器异常", "505");
		}
		return JsonReturnHelper.getSuccessWithData(list);
	}
	
}
